﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using MinhTam.Objects;
using System.Data;
using MinhTam.DataAccess.Web;

/// <summary>
/// Summary description for UserLogin
/// </summary>
public class UserLogin
{
    private static string GetValue(Page P, string field)
    {
        DataTable table = LOG_IN_SESSION.GetTableFields(string.Format("{0} = '{1}'", LOG_IN_SESSION.cl_CLIENT_ID, P.Request.UserHostAddress),
            new string[] { },
        field);
        if ((table == null) || (table.Rows.Count == 0))
        {
            return "";
        }
        return table.Rows[0][field].ToString();
    }
    public static string FULL_NAME(Page P)
    {
        return GetValue(P, LOG_IN_SESSION.cl_FULL_NAME);
    }

    public static string GroupID(Page P)
    {

        return GetValue(P, LOG_IN_SESSION.cl_USER_GROUP_ID);
    }


    public static bool IsAdmin(Page P)
    {

        return GroupID(P) == "1";
    }

    public static bool RemoveLogin(Page P)
    {
        SQLConnectWeb.ExecSQL("DELETE  from LOG_IN_SESSION where  CLIENT_ID='" + P.Request.UserHostAddress.ToString() + "'");
        return true;
    }

    public static bool SaveGroup(Page P, string sGroupID)
    {
        DataTable table = SQLConnectWeb.GetTable("select SESSION_ID from LOG_IN_SESSION where  CLIENT_ID  ='" + P.Request.UserHostAddress.ToString() + "'");
        if ((table == null) || (table.Rows.Count == 0))
        {
            return false;
        }
        string str3 = table.Rows[0]["CLIENT_ID"].ToString();
        return SQLConnectWeb.ExecSQL("UPDATE LOG_IN_SESSION SET USER_GROUP_ID=" + sGroupID + " WHERE CLIENT_ID='" + str3 + "'");
    }

    public static bool SaveUserLogin(Page P, string s_USER_NAME, string sUSER_ID, string sFULL_NAME, string sUSER_GROUP_ID)
    {
        string sCLIENT_ID = P.Request.UserHostAddress.ToString();
        DataTable table = SQLConnectWeb.GetTable("select SESSION_ID from LOG_IN_SESSION where  CLIENT_ID='" + sCLIENT_ID + "'");
        if (table == null)
        {
            return false;
        }
        if (table.Rows.Count == 0)
        {
            return (LOG_IN_SESSION.Insert_Object(sCLIENT_ID, sUSER_ID, s_USER_NAME, sFULL_NAME, sUSER_GROUP_ID) != null);
        }
        LOG_IN_SESSION login = new LOG_IN_SESSION();
        login.SESSION_ID = table.Rows[0][LOG_IN_SESSION.cl_SESSION_ID].ToString();
        return login.Save_Object(sCLIENT_ID, sUSER_ID, s_USER_NAME, sFULL_NAME, sUSER_GROUP_ID);
    }

    public static string USER_ID(Page P)
    {
        return GetValue(P,LOG_IN_SESSION.cl_USER_ID);
    }

    public static string USER_NAME(Page P)
    {
        return GetValue(P, LOG_IN_SESSION.cl_USER_NAME);
    }


    public static bool  IsLogin(Page P)
    {
        return USER_ID(P) != "";
    }

    public static LOG_IN_SESSION GetCurentLogin(Page P)
    {
        DataTable table = LOG_IN_SESSION.GetTableAll(string.Format("{0} = '{1}'", LOG_IN_SESSION.cl_CLIENT_ID, P.Request.UserHostAddress),
          null);
        if ((table == null) || (table.Rows.Count == 0))
        {
            return null;
        }
       return new LOG_IN_SESSION(table,0);
    }
}